﻿package control 
{
	import com.snzflash.events.NavigationEvent;
	import com.snzflash.media.VideoPlayer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * Контрол бар
	 * @author firsoff maxim, tel: +79222345364, skype: maxim.firsoff
	 */
	public class VideoControlBar extends Sprite
	{
		
		// PROPERTIES
	
		// public
		public var playButton:MovieClip;
		public var pauseButton:MovieClip;
		public var back:MovieClip;
		public var light:MovieClip;
		public var normalscreenButton:MovieClip;
		public var fullscreenButton:MovieClip;
		public var muteButton:MovieClip;
		public var unmuteButton:MovieClip;
		public var volumeSlider:VolumeControl
		public var elapsedText:TextField;
		public var totalText:TextField;
		public var trackBar:TrackControl;
		
		
		
		// private
		// громкость - метод обертка
		private var _volume:Number;
		
		// состояния
		private var _state:String = VideoPlayer.PAUSED;
		private var _fullscreen:Boolean = false;
		// == STATIC==
		public static const DEFAULT_VOLUME:Number = 0.6;
		public static const DEFAULT_STATE:String = VideoPlayer.PAUSED;
		
		// CONSTRUCTOR
		public function VideoControlBar() 
		{
			//trace(volumeSlider)
			init();
		};
		
		public function reset():void 
		{
			totalText.text = elapsedText.text = '00:00';
			volumeSlider.position = DEFAULT_VOLUME;
			state = DEFAULT_STATE;
			fullscreen = false;
		};
		
		
		private function init():void
		{
			volumeSlider.addEventListener(Event.CHANGE, onVolumeChange);
			trackBar.addEventListener(Event.CHANGE, onSeek);
			
			playButton.addEventListener(MouseEvent.CLICK, onPlayBtnClick);
			pauseButton.addEventListener(MouseEvent.CLICK, onPauseBtnClick);
			
			normalscreenButton.addEventListener(MouseEvent.CLICK, onNormalscreenBtnClick);
			fullscreenButton.addEventListener(MouseEvent.CLICK, onFullscreenBtnClick);
			
			reset();
		};
		
		// METHODS
	
		// public
		
		// private
		
		// getters/setters
		public function get state():String { return _state; };
		
		public function set state(value:String):void 
		{
			//trace(value)
			switch (value) 
			{
				case VideoPlayer.PLAYING:
					_state = value;
					pauseButton.visible = true;
					playButton.visible = false;
				break;
				
				case VideoPlayer.PAUSED:
					_state = value;
					pauseButton.visible = false;
					playButton.visible = true;
				break;
			}
		};
		
		public function get fullscreen():Boolean { return _fullscreen; };
		
		public function set fullscreen(value:Boolean):void 
		{
			_fullscreen = value;
			normalscreenButton.visible = value;
			fullscreenButton.visible = !value;
		};
		
		public function get volume():Number { return _volume; };
		
		public function set volume(value:Number):void 
		{
			value  = value < 0 ? 0 : value;
			value  = value > 1 ? 1 : value;
			_volume = value;
			volumeSlider.position = value;
		};
		
		
		// == STATIC==
		
		
		// HANDLERS
		
		/**
		 * При изменении громкости
		 * @param	event
		 */
		private function onVolumeChange(event:Event):void 
		{
			var e:NavigationEvent = new NavigationEvent(NavigationEvent.VOLUME, false, false, volumeSlider.position);
			dispatchEvent(e);
		};
		
		
		
		private function onPauseBtnClick(event:MouseEvent):void 
		{
			state = VideoPlayer.PAUSED;
			dispatchEvent(new NavigationEvent(NavigationEvent.PAUSE));
		};
		
		private function onPlayBtnClick(event:MouseEvent):void 
		{
			state = VideoPlayer.PLAYING;
			dispatchEvent(new NavigationEvent(NavigationEvent.PLAY));
		};
		
		private function onFullscreenBtnClick(event:MouseEvent):void 
		{
			fullscreen = true;
			dispatchEvent(new NavigationEvent(NavigationEvent.FULLSCREEN));
		};
		
		private function onNormalscreenBtnClick(event:MouseEvent):void 
		{
			fullscreen = false;
			dispatchEvent(new NavigationEvent(NavigationEvent.NORMAL_SCREEN));
		};
		
		private function onSeek(event:Event):void 
		{
			var e:NavigationEvent = new NavigationEvent(NavigationEvent.SEEK, false, false, trackBar.playProgress);
			dispatchEvent(e);
		};
	};

};